#!/bin/sh
# 校验sdk 是否被其他项目编译过，编译过则还原设置

set -e

#当前编译值
status=0

if [ ! -n "$CORELINK_ENV_TUYA_PROJECT_REALPATH" ]; then
    echo "请重新执行 env : source ./common/env.sh"
    exit 1
fi

if [ $CORELINK_ENV_TUYA_PROJECT_REALPATH != $(realpath "./") ]; then
    echo "请重新执行 env : source ./common/env.sh"
    exit 1
fi

if [ ! -e $CORELINK_ENV_TUYA_SDK_REALPATH/.corelink_record_project ]; then
    echo "sdk的文件将被还原到初始状态 :s1"
    status=1
else
    tmp_val=$(cat $CORELINK_ENV_TUYA_SDK_REALPATH/.corelink_record_project)
    if [ $CORELINK_ENV_TUYA_PROJECT_REALPATH = $tmp_val ]; then
        # 普通正常逻辑
        echo "正常同项目编译:s1"
    else
        echo "不同项目重新编译:s1"
        status=1
    fi
fi

build_model_name=$1
echo $build_model_name

if [ ! -e $CORELINK_ENV_TUYA_SDK_REALPATH/.corelink_build_model_name ]; then
    echo "sdk的文件将被还原到初始状态 :s2"
    status=1
else
    tmp_val=$(cat $CORELINK_ENV_TUYA_SDK_REALPATH/.corelink_build_model_name)
    if [ $tmp_val = $build_model_name ]; then
        # 普通正常逻辑
        echo "正常同项目编译:s2"
    else
        echo "不同项目重新编译:s2"
        status=1
    fi
fi


if [ $status = 1 ]; then
    ./common/checkout_tuya_sdk.sh
    echo "$CORELINK_ENV_TUYA_PROJECT_REALPATH" > $CORELINK_ENV_TUYA_SDK_REALPATH/.corelink_record_project
    echo "$build_model_name" > $CORELINK_ENV_TUYA_SDK_REALPATH/.corelink_build_model_name
fi
